var pageNum;
$(function () {

    getQaTypeList(1);
    $('body').on('click', '#btn1,#btn2',function () {//条件查询分类列表
        getQaTypeList(1);
    }).on('click','.fa-eye',function () {
        var qaTypeId = $(this).parent().data('id');
        parent.layer.open({
            type: 2,
            area: ['900px', '520px'],
            content: baseloaction + '/admin/qa/qatype/qaTypeInfo?qaTypeId=' + qaTypeId,
            maxmin: true,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '<strong>查看问答分类</strong>',
            btn:["进入修改状态","取消"],
            yes: function (index, layero) {
                window.location.href = baseloaction + '/admin/qa/qatype/initUpdate?qaTypeId=' + qaTypeId;
                parent.layer.close(index);
            }
        });
    }).on('click','.fa-history',function () {//恢复
        var obj = $(this).parents('tr');
        updateQaTypeStatus(obj, 1);
    }).on('click','#enable-batch',function () {//批量恢复
        var obj = $('input[name=qaTypeId]:checked').parents('tr');
        updateQaTypeStatus(obj, 1);
    }).on('click', '.fa-unlock-alt', function () {//禁用
        var obj = $(this).parents('tr');
        updateQaTypeStatus(obj, 2);
    }).on('click', '#disable-batch', function () {//批量禁用
        var obj = $('input[name=qaTypeId]:checked').parents('tr');
        updateQaTypeStatus(obj, 2);
    }).on('click','.fa-trash-o',function () {//删除
        var obj = $(this).parents('tr');
        updateQaTypeStatus(obj, 3);
    }).on('click','#delete-batch',function () {//批量删除
        var obj = $('input[name=qaTypeId]:checked').parents('tr');
        updateQaTypeStatus(obj, 3);
    });

    /**
     * 查询问答分类列表
     * @param pageNo
     */
    function getQaTypeList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/qa/qatype/ajaxQueryQaTypeList',//请求URLt
            contextId: 'oc-ibox-content',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getQaTypeList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-qaTag', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#type-count").html(total);
            }
        };
        ajaxGetListData(obj);
    }

    /**
     * 修改分类的状态
     * @param obj
     * @param status
     */
    function updateQaTypeStatus(obj, status) {
        var message = '';
        if(status==1){
            message = '恢复';
        }else if(status == 2){
            message = '禁用';
        }else if(status == 3 ){
            message = '删除';
        }

        if($.isEmptyObject(obj) || obj.length <= 0){
            icModale(0, '请选择要'+ message +'的数据');return;
        }
        icModale(2,"确定要"+ message +"所选的类型吗?",3,function () {
            var qaTypeIds = obtainIds(obj);
            $.ajax({
                url: baseloaction + '/admin/qa/qatype/updateQaTypeStatus',
                data:{"qaTypeIds": qaTypeIds, "status":status},
                type:"post",
                dataType:"json",
                success: function (result) {
                    icModale(0, result.message);
                    if(result.success){
                        if(status == 1){
                            obj.find('td[qaTypeStatus]').html('<span class="label label-info">正常</span>');
                            obj.find('a[qaTypeStatus]').attr('data-original-title','禁用').html('<i class="fa fa-unlock-alt"></i>');
                        }else if(status == 2){
                            obj.find('td[qaTypeStatus]').html('<span class="label label-danger">禁用</span>');
                            obj.find('a[qaTypeStatus]').attr('data-original-title','恢复').html('<i class="fa fa-history"></i>');
                        }else if(status == 3){
                            getQaTypeList(pageNum);
                        }
                    }
                },
                error: function (error) {
                    icModale(0, '系统繁忙，数据'+ message +'失败');
                }
            })
        });
    }

})

//拼接id串
function obtainIds(obj) {
    if(null == obj || obj.size() == 0){
        icModale(0,"请选择要操作的数据");
        return null;
    }
    var qaTypeIds = "";
    $(obj).each(function (index,_input) {
        qaTypeIds += $(_input).data('id') + ",";
    })
    qaTypeIds = qaTypeIds.substring(0,qaTypeIds.length-1);
    return qaTypeIds;
}